﻿using Bon.It.Proxy;
using Bon_It.Model;
using Bon_It.Model.TransferObjects;

namespace Bonit_It.ViewModel.Cashier
{

    /// <summary>
    /// An engine for the commandinput.
    /// </summary>
    public class CommandInputEngine
    {

        /// <summary>
        /// Gets or sets the input mode.
        /// </summary>
        /// <value>
        /// The input mode.
        /// </value>
        public static InputModes InputMode { get; set; }

        /// <summary>
        /// The input modes
        /// </summary>
        public enum InputModes
        {
            /// <summary>
            /// Unknownmode
            /// </summary>
            Unknown =0,

            /// <summary>
            /// add articles and modifying them.
            /// </summary>
            ArticleSelection = 2,

            /// <summary>
            /// payment
            /// </summary>
            Payment = 4, 

        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CommandInputEngine"/> class.
        /// </summary>
        public CommandInputEngine()
        {
            InputMode= InputModes.ArticleSelection;
        }

        /// <summary>
        /// Parse the Article command and result a <see cref="BookingItem"/>
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public BookingItem GetBookingItem(string inputString)
        {
            Artikel article = GetArticle(inputString);
            decimal amount = GetAmount(inputString);

            if (article == null)
                return null;

            return new BookingItem(article, amount);
        }

        /// <summary>
        /// Get the Amount input from the <see cref="inputString"/>
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        private decimal GetAmount(string inputString)
        {
            decimal amount;
             string[] splits = inputString.Split('*');
             if (splits.Length == 2)
             {
                 if (decimal.TryParse(splits[0], out amount))
                 {
                     return amount;
                 }
             }
            return 1;
        }

        /// <summary>
        /// Parse the Article EAN-Number from the input string and returns the <see cref="Artikel"/> - Object.
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        private Artikel GetArticle(string inputString)
        {
            long ean = 0;

            string eanString = inputString;
            if (inputString.Contains("*"))
            {
                string[] splits = inputString.Split('*');
                if (splits.Length == 2)
                {
                    eanString = splits[1];
                }
            }

                
            if (long.TryParse(eanString, out ean))
            {
                return ServiceProxy.Instance.GetArtikelByEan(ean);
            }
            return null;
        }
    }
}
